package logic

import (
	"context"

	"game/api/internal/svc"
	"game/api/internal/types"
	"game/rpc/gamerpc/game"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
	return &CreateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) (*types.CreateUserResponse, error) {
	// 调用 rpc 服务
	rpcReq := &game.CreateUserRequest{
		User: &game.User{
			Username: req.User.Username,
			Password: req.User.Password,
			Email:    req.User.Email,
		},
	}

	rpcResp, err := l.svcCtx.GameRpc.CreateUser(l.ctx, rpcReq)
	if err != nil {
		return nil, err
	}

	// 构造 api 响应
	apiResp := &types.CreateUserResponse{
		Id: rpcResp.Id,
	}

	return apiResp, nil
}
